/**
 * \file
 *
 * \brief User board initialization template
 *
 */

#include <asf.h>
#include "user_board.h"
#include <board.h>
#include <conf_board.h>

void board_init(void)
{
	/* This function is meant to contain board-specific initialization code
	 * for, e.g., the I/O pins. The initialization can rely on application-
	 * specific board configuration, found in conf_board.h.
	 */
	//TODO: explore when this function is needed
	ioport_init();
	
	// initialize ADC Current sensor
	ioport_set_pin_dir(CURRENT_SENSOR_SIGNAL_PIN, IOPORT_DIR_INPUT);
	
	// initialize Vienna RobotChallenge IR module
	ioport_configure_pin(REMOTE_START, IOPORT_DIR_INPUT);
	ioport_configure_pin(REMOTE_KILL, IOPORT_DIR_INPUT);

	// initialize buttons
	ioport_configure_pin(MY_BUTTON1, IOPORT_DIR_INPUT | IOPORT_PULL_UP);
	ioport_configure_pin(MY_BUTTON2, IOPORT_DIR_INPUT | IOPORT_PULL_UP);
	ioport_configure_pin(MY_BUTTON3, IOPORT_DIR_INPUT | IOPORT_PULL_UP);
	ioport_configure_pin(MY_BUTTON4, IOPORT_DIR_INPUT | IOPORT_PULL_UP);
	ioport_configure_pin(MY_BUTTON5, IOPORT_DIR_INPUT | IOPORT_PULL_UP);
	
	// initialize encoders
	ioport_set_pin_dir(ENCODER_L, IOPORT_DIR_INPUT);
	ioport_set_pin_dir(ENCODER_R, IOPORT_DIR_INPUT);
	
	// initialize motors
	ioport_configure_pin(MOTOR_L1, IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW);
	ioport_configure_pin(MOTOR_L2, IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH);
	ioport_configure_pin(MOTOR_L3, IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH);
	ioport_configure_pin(MOTOR_L4, IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW);
	ioport_configure_pin(MOTOR_R1, IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW);
	ioport_configure_pin(MOTOR_R2, IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH);
	ioport_configure_pin(MOTOR_R3, IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH);
	ioport_configure_pin(MOTOR_R4, IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW);

	// initialize servo motors
	//ioport_configure_pin(SERVO_R, IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW);
	ioport_configure_pin(SERVO_L, IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW);

	// initialize line sensors
	ioport_set_port_dir(LINE_SENSOR_PORT, 0b00001111, IOPORT_DIR_INPUT);
	
	// initialize IR module
	ioport_configure_pin(KILL_SW, IOPORT_DIR_INPUT);
	ioport_configure_pin(START_SW, IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH);
	
	// initialize sensor power
	//ioport_set_port_dir(POWER_PORT, 0b00111110, IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW);
	ioport_configure_group(POWER_PORT, 0b00111110, IOPORT_DIR_OUTPUT | IOPORT_PULL_DOWN);
	
	// initialize distance sensors
	ioport_set_port_dir(PANASONIC_PORT, 0b11111111, IOPORT_DIR_INPUT);
	ioport_set_pin_dir(PEPPERL_FUCHS, IOPORT_DIR_INPUT);
	
	// initialize rotary switch
	ioport_set_port_dir(ROTARY_PORT, 0b00111100, IOPORT_DIR_INPUT);


	// LCD controller init (NHD-C12832A1Z-FSW-FBW-3V3)
	ioport_configure_pin(NHD_C12832A1Z_SPI_SCK, IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH);
	ioport_configure_pin(NHD_C12832A1Z_SPI_MOSI, IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH);
	ioport_configure_pin(NHD_C12832A1Z_CSN, IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH);
	ioport_configure_pin(NHD_C12832A1Z_REGISTER_SELECT, IOPORT_DIR_OUTPUT | IOPORT_PULL_UP);	// pull-up
	ioport_configure_pin(NHD_C12832A1Z_RESETN, IOPORT_DIR_OUTPUT | IOPORT_PULL_UP);			// pull-up
	ioport_configure_pin(NHD_C12832A1Z_BACKLIGHT, IOPORT_DIR_OUTPUT | IOPORT_INIT_LOW);
	ioport_configure_pin(LED_LATCH, IOPORT_DIR_OUTPUT | IOPORT_PULL_UP);
	ioport_configure_port_pin(&PORTF, PIN6_bm, IOPORT_DIR_INPUT);	// Not used MISO

	
	// USARTD1
	ioport_configure_pin(IOPORT_CREATE_PIN(PORTD,7), IOPORT_DIR_OUTPUT | IOPORT_INIT_HIGH);
	ioport_configure_pin(IOPORT_CREATE_PIN(PORTD,6), IOPORT_DIR_INPUT);
}
